package inseniarlingua.lang.en;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import inseniarlingua.tokenizer.Word;
import inseniarlingua.tokenizer.TokenizerBase;

public class TokenizerEN extends TokenizerBase
{
	private static Map<String,List<Word>> quoteWords;
	private Map<String,Object> transMap;
	
	public void setTransMap(Map<String, Object> transMap) {
		this.transMap = transMap;
	}

	public static Map<String,List<Word>> getQuoteWords() {
		if (quoteWords == null) {
			quoteWords = new LinkedHashMap<String, List<Word>>();
			quoteWords.put("I'm", Arrays.asList(new Word("I"), new Word("am")));
			quoteWords.put("we're", Arrays.asList(new Word("we"), new Word("are")));
			quoteWords.put("you're", Arrays.asList(new Word("you"), new Word("are")));
			quoteWords.put("o'clock", Arrays.asList(new Word("o'clock")));
			quoteWords.put("can't", Arrays.asList(new Word("can not")));
			quoteWords.put("it's", Arrays.asList(new Word("it"), new Word("is")));
			quoteWords.put("that's", Arrays.asList(new Word("that"), new Word("is")));
			quoteWords.put("wouldn't", Arrays.asList(new Word("would"), new Word("not")));
			quoteWords.put("I'll", Arrays.asList(new Word("I"), new Word("will")));
			
			// n't -> not
			String [] notWords = { 
					"didn't", "don't", "doesn't",
					"hasn't", "haven't",
					"shouldn't",
			};
			for (String w : notWords) {
				String w1 = w.substring(0, w.length()-3);
				quoteWords.put(w, Arrays.asList(new Word(w1), new Word("not")));
			}
			System.out.println(quoteWords);
		}
		return quoteWords;
	}
	
	@Override
	public List<Word> tokenize() {
		List<Word> tokens = super.tokenize();
		//System.out.println("super.tokenize() = " + tokens);
		Stack<Word> ret = new Stack<Word>();
		int itoken = 0;
		Word pToken = Word.NULL_WORD;
		Word ppToken = Word.NULL_WORD;
		while (itoken < tokens.size()) {
			Word token = tokens.get(itoken);			
			if (pToken.getValue().equals("'")) {
				String word3 = ppToken.getValue()+pToken.getValue()+token.getValue();
				List<Word> quoteWords = getQuoteWords().get(word3);
				if (quoteWords != null) {
					ret.pop(); ret.pop();
					ret.addAll(quoteWords);
				}
				else if ("s".equals(token.getValue())) {
					ret.set(ret.size()-1, new Word("'s"));
				}
			}
			else if (token.getValue().equals(".")) {
				String abrWord = pToken.getValue()+token.getValue();
				if (transMap.containsKey(abrWord)) {
					ret.pop();
					ret.add(new Word(abrWord));
				}
			}
			else {
				int ibeg = Math.max(0, itoken-3);
				boolean phraseFound = false;
				for (int i=ibeg; i<itoken; i++) {
					String phrase = joinWords(tokens, i, itoken+1);
					if (transMap.containsKey(phrase)) {
						for (int j=i; j<itoken; j++) {
							ret.pop();							
						}
						ret.add(new Word(phrase));
						phraseFound = true;
						break;
					}
				}
				if (! phraseFound) {
					ret.add(token);
				}
			}
			ppToken = pToken;
			pToken = token;
			itoken++;
		}
		return ret;
	}

}
